package com.expdatacloud.controller.itrn;

import com.expdatacloud.dto.ApiResponse;
import com.expdatacloud.service.YtBinitrnService;
import com.expdatacloud.utils.PdfReportUtil;
import com.expdatacloud.vo.SumItrnVo;
import com.expdatacloud.vo.YtBinitrnVo;
import com.expdatacloud.vo.YtLinetupVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * (YtBinitrnVo)表控制层
 *
 * @author makejava
 * @since 2020-05-22 15:12:20
 */
@RestController
@RequestMapping("ytBinitrn")
public class YtBinitrnController {
    /**
     * 服务对象
     */
    @Resource
    private YtBinitrnService ytBinitrnService;

    /**
     *  bin卡表头数据查询
     * */
    @GetMapping("/itrnSummarizingList")
    public ApiResponse itrnSummarizingList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,YtBinitrnVo ytBinitrnVo){
        PageHelper.startPage(page, limit);
        PageInfo<SumItrnVo> pageInfo = new PageInfo<>(ytBinitrnService.itrnSummarizingList(ytBinitrnVo));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     *  bin卡明细数据查询
     * */
    @GetMapping("/binitrnList")
    public ApiResponse binitrnList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, HttpSession session, YtBinitrnVo ytBinitrnVo){
        // 查询数据
        List<YtBinitrnVo> list = ytBinitrnService.binitrnList(ytBinitrnVo);

        // 存入session用于导出
        session.setAttribute("binitrnList",list);

        PageHelper.startPage(page, limit);
        PageInfo<YtBinitrnVo> pageInfo = new PageInfo<>(list);
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     *  获取逻辑仓
     * */
    @GetMapping("/getWhcode")
    public ApiResponse getWhcode(String storerkey){
        return ApiResponse.ok(ytBinitrnService.getWhcodeList(storerkey));
    }

    /**
     *  获取库位和储位
     * */
    @GetMapping("/getLoc")
    public ApiResponse getLoc(YtLinetupVo ytLinetupVo){
        return ApiResponse.ok(ytBinitrnService.getLocList(ytLinetupVo));
    }

    @RequestMapping("/scorePDF")
    public Object scorePDF (@RequestParam("nums") Object nums, HttpServletResponse response) throws Exception {
        String datas = nums.toString();
        String[] str = datas.split(",");
        List<String> data = new ArrayList<String>();
        for (int i = 0; i < str.length; i++) {
            data.add(str[i]);
        }

        //用于存储listScore的每个for循环的值
        List list = new ArrayList<>();
        //查找选中的成绩信息
        List<YtLinetupVo> listScore = ytBinitrnService.getItrn(data);
        for (YtLinetupVo ext : listScore) {
            String[] arr = {ext.getLoc(),ext.getLoc(),ext.getLoc(),ext.getLoc(),ext.getLoc()};
            list.add(arr);
        }


        try {
            Document document = new Document(new Rectangle(520,200));
            File f = File.createTempFile("Pdf生成", ".pdf");
            PdfWriter.getInstance(document, new FileOutputStream(f));

            document.open();

            for(int a = 0; a < list.size(); a++){
                // 分页
                if(a > 0){
                    document.newPage();
                }

                // 创建表头
                PdfPTable table1 = PdfReportUtil.createTable(5);
                String[] ths = {"学号", "姓名", "课程", "成绩", "类型"};
                for (int i = 0; i < ths.length; i++) {
                    // 为单元格填充数据
                    table1.addCell(PdfReportUtil.createCell(ths[i]));
                }

                // 创建明细
                PdfPTable table2 = PdfReportUtil.createTable(5);
                String[] arr = (String[]) list.get(a);
                for (int j = 0; j < arr.length; j++) {
                    if(j == 3 || j == 4){
                        // 生成二维码/条形码流
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        ImageIO.write(j == 3? PdfReportUtil.getQrCode(arr[j]) : PdfReportUtil.getCode128(arr[j]),"png",out);
                        byte[] b = out.toByteArray();

                        Image code = Image.getInstance(b);

                        table2.addCell(PdfReportUtil.createCell(code));
                    }else {
                        table2.addCell(PdfReportUtil.createCell(arr[j]));
                    }
                }

                document.add(table1);
                document.add(table2);
            }

            // 关闭document
            document.close();

            ByteArrayOutputStream bos = PdfReportUtil.genByteArrayOutputStream(f);

            response.getOutputStream().write(bos.toByteArray());
            return null;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

}